package com.ruoyi.airspaceFlight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.airspaceFlight.domain.TAirspaceSettings;
import com.ruoyi.airspaceFlight.domain.TAirspaceType;
import com.ruoyi.airspaceFlight.domain.TLowairServiceFile;
import com.ruoyi.airspaceFlight.dto.req.AirspaceSetReqDto;
import com.ruoyi.airspaceFlight.dto.req.StatusDto;
import com.ruoyi.airspaceFlight.dto.req.TAirspaceSettingsDto;
import com.ruoyi.airspaceFlight.dto.req.TLowairServiceFileDto;
import com.ruoyi.airspaceFlight.dto.resp.AirspaceRespOneDto;
import com.ruoyi.airspaceFlight.dto.resp.AirspaceSetRespDto;
import com.ruoyi.airspaceFlight.mapper.TAirspaceSettingsMapper;
import com.ruoyi.airspaceFlight.mapper.TAirspaceTypeMapper;
import com.ruoyi.airspaceFlight.mapper.TLowairServiceFileMapper;
import com.ruoyi.airspaceFlight.service.TAirspaceSettingsService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dell
 * @description 针对表【t_airspace_settings(空域设置信息表)】的数据库操作Service实现
 * @createDate 2023-11-02 10:52:23
 */
@Service
public class TAirspaceSettingsServiceImpl extends ServiceImpl<TAirspaceSettingsMapper, TAirspaceSettings>
        implements TAirspaceSettingsService {

    @Autowired
    private TAirspaceSettingsMapper tAirspaceSettingsMapper;

    @Autowired
    private TLowairServiceFileMapper tLowairServiceFileMapper;

    @Autowired
    private TAirspaceTypeMapper tAirspaceTypeMapper;

    @Override
    public List<AirspaceRespOneDto> getAirspace(List<String> airTypeIds) {

        List<AirspaceRespOneDto> list = new ArrayList<>();

        for (String airTypeId : airTypeIds) {

            List<TAirspaceSettings> tAirspaceSettingsList = tAirspaceSettingsMapper.selectList(new LambdaQueryWrapper<TAirspaceSettings>().eq(TAirspaceSettings::getAirTypeId, airTypeId));

            for (TAirspaceSettings tAirspaceSettings : tAirspaceSettingsList) {
                AirspaceRespOneDto airspaceRespOneDto = new AirspaceRespOneDto();
                BeanUtils.copyBeanProp(airspaceRespOneDto, tAirspaceSettings);

                TAirspaceType tAirspaceType = tAirspaceTypeMapper.selectById(airTypeId);
                BeanUtils.copyBeanProp(airspaceRespOneDto, tAirspaceType);

                List<TLowairServiceFile> tLowairServiceFiles = tLowairServiceFileMapper.selectList(new LambdaQueryWrapper<TLowairServiceFile>().eq(TLowairServiceFile::getBusinessId, tAirspaceSettings.getAirspaceId()));
                airspaceRespOneDto.setFiles(tLowairServiceFiles);
                list.add(airspaceRespOneDto);
            }

//            TAirspaceSettings tAirspaceSettings = tAirspaceSettingsMapper.selectById(airspaceId);
//            BeanUtils.copyBeanProp(airspaceRespOneDto, tAirspaceSettings);
//
//            TAirspaceType tAirspaceType = tAirspaceTypeMapper.selectById(tAirspaceSettings.getAirTypeId());
//            BeanUtils.copyBeanProp(airspaceRespOneDto, tAirspaceType);
//
//            List<TLowairServiceFile> tLowairServiceFiles = tLowairServiceFileMapper.selectList(new LambdaQueryWrapper<TLowairServiceFile>().eq(TLowairServiceFile::getBusinessId, airspaceId));
//
//            airspaceRespOneDto.setFiles(tLowairServiceFiles);
//            list.add(airspaceRespOneDto);
        }

        return list;
    }

    @Override
    public List<AirspaceSetRespDto> selectTAirspaceSettingsList(AirspaceSetReqDto airspaceSetReqDto) {

        return tAirspaceSettingsMapper.selectTAirspaceSettingsList(airspaceSetReqDto);
    }

    @Override
    public AirspaceRespOneDto selectTAirspaceSettingsByAirspaceId(String airspaceId) {
        AirspaceRespOneDto airspaceRespOneDto = new AirspaceRespOneDto();
        TAirspaceSettings tAirspaceSettings = tAirspaceSettingsMapper.selectById(airspaceId);
        BeanUtils.copyBeanProp(airspaceRespOneDto, tAirspaceSettings);

        TAirspaceType tAirspaceType = tAirspaceTypeMapper.selectById(tAirspaceSettings.getAirTypeId());
        BeanUtils.copyBeanProp(airspaceRespOneDto, tAirspaceType);

        airspaceRespOneDto.setRemark(tAirspaceSettings.getRemark());

        List<TLowairServiceFile> tLowairServiceFiles = tLowairServiceFileMapper.selectList(new LambdaQueryWrapper<TLowairServiceFile>().eq(TLowairServiceFile::getBusinessId, airspaceId));

        airspaceRespOneDto.setFiles(tLowairServiceFiles);

        return airspaceRespOneDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertTAirspaceSettings(TAirspaceSettingsDto tAirspaceSettingsDto) {

        TAirspaceSettings tAirspaceSettings = new TAirspaceSettings();
        BeanUtils.copyBeanProp(tAirspaceSettings, tAirspaceSettingsDto);
        tAirspaceSettings.setCreateTime(DateUtils.getNowDate());
        tAirspaceSettings.setCreateBy(SecurityUtils.getUsername());
        tAirspaceSettings.setUpdateTime(DateUtils.getNowDate());
        tAirspaceSettings.setUpdateBy(SecurityUtils.getUsername());

        String airspaceName = tAirspaceSettings.getAirspaceName();
        List<String> list = tAirspaceSettingsMapper.selectAllAirspaceName();

        if (list.contains(airspaceName)){
            return AjaxResult.error("空域名称已存在");
        }

        if (tAirspaceSettings.getStatus() == 6){
            //0-生效中 1-待生效 2-已停用 3-已到期
            if (tAirspaceSettings.getStartTime().after(DateUtils.getNowDate())) {
                tAirspaceSettings.setStatus(1);
            } else if (tAirspaceSettings.getEndTime().before(DateUtils.getNowDate())) {
                tAirspaceSettings.setStatus(3);
            } else if (tAirspaceSettings.getStartTime().before(DateUtils.getNowDate()) && tAirspaceSettings.getEndTime().after(DateUtils.getNowDate())) {
                tAirspaceSettings.setStatus(0);
            } else if (tAirspaceSettings.getStartTime().equals(DateUtils.getNowDate()) || tAirspaceSettings.getEndTime().equals(DateUtils.getNowDate())){
                tAirspaceSettings.setStatus(0);
            }
        }

        tAirspaceSettingsMapper.insert(tAirspaceSettings);

        List<TLowairServiceFileDto> files = tAirspaceSettingsDto.getFiles();
        for (TLowairServiceFileDto file : files) {
            TLowairServiceFile tLowairServiceFile = new TLowairServiceFile();
            BeanUtils.copyBeanProp(tLowairServiceFile, file);
            tLowairServiceFile.setBusinessId(tAirspaceSettings.getAirspaceId());
            tLowairServiceFile.setCreateTime(DateUtils.getNowDate());
            tLowairServiceFile.setCreateBy(SecurityUtils.getUsername());
            tLowairServiceFile.setUpdateTime(DateUtils.getNowDate());
            tLowairServiceFile.setUpdateBy(SecurityUtils.getUsername());
            tLowairServiceFileMapper.insert(tLowairServiceFile);
        }

        return AjaxResult.success("新增成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateTAirspaceSettings(TAirspaceSettingsDto tAirspaceSettingsDto) {

        TAirspaceSettings tAirspaceSettings = new TAirspaceSettings();
        BeanUtils.copyBeanProp(tAirspaceSettings, tAirspaceSettingsDto);
        tAirspaceSettings.setUpdateTime(DateUtils.getNowDate());
        tAirspaceSettings.setUpdateBy(SecurityUtils.getUsername());

        String airspaceName = tAirspaceSettings.getAirspaceName();
        List<String> list = tAirspaceSettingsMapper.selectAllAirspaceNameExSelf(tAirspaceSettings.getAirspaceId());

        if (list.contains(airspaceName)){
            return AjaxResult.error("空域名称已存在");
        }

        if (tAirspaceSettings.getStatus() == 6){
            //0-生效中 1-待生效 2-已停用 3-已到期
            if (tAirspaceSettings.getStartTime().after(DateUtils.getNowDate())) {
                tAirspaceSettings.setStatus(1);
            } else if (tAirspaceSettings.getEndTime().before(DateUtils.getNowDate())) {
                tAirspaceSettings.setStatus(3);
            } else if (tAirspaceSettings.getStartTime().before(DateUtils.getNowDate()) && tAirspaceSettings.getEndTime().after(DateUtils.getNowDate())) {
                tAirspaceSettings.setStatus(0);
            } else if (tAirspaceSettings.getStartTime().equals(DateUtils.getNowDate()) || tAirspaceSettings.getEndTime().equals(DateUtils.getNowDate())){
                tAirspaceSettings.setStatus(0);
            }
        }

        tAirspaceSettingsMapper.updateById(tAirspaceSettings);

        List<TLowairServiceFileDto> files = tAirspaceSettingsDto.getFiles();
//        List<TLowairServiceFile> list = new ArrayList<>();
        for (TLowairServiceFileDto file : files) {
            TLowairServiceFile tLowairServiceFile = new TLowairServiceFile();
            BeanUtils.copyBeanProp(tLowairServiceFile, file);
            tLowairServiceFile.setBusinessId(tAirspaceSettings.getAirspaceId());
            if (StringUtils.isNull(tLowairServiceFile.getFileId())){
                tLowairServiceFile.setCreateTime(DateUtils.getNowDate());
                tLowairServiceFile.setCreateBy(SecurityUtils.getUsername());
                tLowairServiceFile.setUpdateTime(DateUtils.getNowDate());
                tLowairServiceFile.setUpdateBy(SecurityUtils.getUsername());
                tLowairServiceFileMapper.insert(tLowairServiceFile);
            }else {
                tLowairServiceFile.setUpdateTime(DateUtils.getNowDate());
                tLowairServiceFile.setUpdateBy(SecurityUtils.getUsername());
                tLowairServiceFileMapper.updateById(tLowairServiceFile);
            }
        }

        return AjaxResult.success("修改成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int onOffTAirspaceSettings(StatusDto statusDto) {

        TAirspaceSettings tAirspaceSettings = tAirspaceSettingsMapper.selectById(statusDto.getAirspaceId());

        if (statusDto.getStatus() == 6){
            //0-生效中 1-待生效 2-已停用 3-已到期
            if (tAirspaceSettings.getStartTime().after(DateUtils.getNowDate())) {
                tAirspaceSettings.setStatus(1);
            } else if (tAirspaceSettings.getEndTime().before(DateUtils.getNowDate())) {
                tAirspaceSettings.setStatus(3);
            } else if (tAirspaceSettings.getStartTime().before(DateUtils.getNowDate()) && tAirspaceSettings.getEndTime().after(DateUtils.getNowDate())) {
                tAirspaceSettings.setStatus(0);
            } else if (tAirspaceSettings.getStartTime().equals(DateUtils.getNowDate()) || tAirspaceSettings.getEndTime().equals(DateUtils.getNowDate())){
                tAirspaceSettings.setStatus(0);
            }
        }else {
            tAirspaceSettings.setStatus(statusDto.getStatus());
        }

        return tAirspaceSettingsMapper.updateById(tAirspaceSettings);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTAirspaceSettingsByAirspaceId(String airspaceId) {

        tLowairServiceFileMapper.delete(new LambdaQueryWrapper<TLowairServiceFile>().eq(TLowairServiceFile::getBusinessId, airspaceId));

        return tAirspaceSettingsMapper.deleteById(airspaceId);
    }
}




